<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class news extends CI_Controller{
      protected $_templates;
      function __construct() {
		parent::__construct();
		$this->pre_message = "";      
		$this->session->set_userdata(array('Url'=>uri_string()));
		$this->lang->load('news');
        $this->load->library('image_library');
		$this->load->library('string_library');
		$this->load->model('news_model','news');
		$this->load->model('category_news_model','category_news');
		$this->load->helper('upload_file');
      }   
	  function index(){
		  $this->acl->check('view','mod_news','news',base_url());
		  $this->listnews();
	  }
      function listnews(){
		 // check quyền xem
         $this->acl->check('view','mod_news','news',base_url());
        /*Begin search*/
            $type = $this->input->post('type');
            $data['search_bar']=searchBar(array(),array('name'=>'Tên chuyên mục','title'=>'Tiêu đề'),'news/listnews/');
            if($type=='ajax')
            {
                $arr_search['key_search'] = $this->input->post('key_search');
                $arr_search['field_search'] = $this->input->post('field_search');
                $this->session->set_userdata('key_search',$arr_search['key_search']);
                $this->session->set_userdata('field_search',$arr_search['field_search']);
               
            }
            else{
                $arr_search['key_search']=$this->session->userdata('key_search');
                $arr_search['field_search']=$this->session->userdata('field_search');
            }
			
            /*End  search*/
            $data['title'] = 'Danh sách bài viết';
			if($this->acl->check('add','mod_news','news'))
            	$data['add'] = 'news/add';
            $config['base_url'] = base_url().'news/listnews/'.(int)$this->uri->segment(3);  
            $data['num'] = $this->news->get_num_data($arr_search);
           	$config['total_rows']   =  $data['num'];
         	$config['per_page']  =   '20';
           	$config['uri_segment'] = 4;   
            $this->pagination->initialize($config);
            $data['list'] =   $this->news->get_all_data($config['per_page'],(int)$this->uri->segment('4'),$arr_search);
			
			$data['pagination']    = $this->pagination->create_links();       
            $this->_templates['page'] = 'news/listnews';
           if($type=="ajax")
                echo $this->load->view($this->_templates['page'],$data,true);
            else
                    $this->site_lib->load($this->_templates['page'],$data); 
      }
    
      function add(){ 
	  		// check quyền thêm
	  		$this->acl->check('add','mod_news','news',base_url());                        
			$data['title'] = 'Thêm mới bài viết';
			$data['option_pid'] = $this->category_news->optionCategoryLevel();
			$this->form_validation->set_rules('category_id',lang('news_category_name'),'trim|required');
			$this->form_validation->set_rules('title',lang('news_name'),'trim|required');
			$this->form_validation->set_rules('description',lang('news_description'),'trim|required');
			$this->form_validation->set_rules('content',lang('news_content'),'trim|required');
			// ghi lai file cau hinh duong dan cua tinymce
		
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
				 $dirFile = "../uploads/images/images_lib/";
				 $dirFileNew = "../uploads/images/news/small/";
				 $dirFileNewHome = "../uploads/images/news/home/";
				 $filename =$_POST['lb_image'];
				 $dirFileRoot =$dirFile.$filename;
				 $dirFileResizeSmall = $dirFileNew.$filename;
				 $dirFileResizeHome = $dirFileNewHome.$filename;
				 $img_name =$this->image_library->resizeThumbnailImage($dirFileResizeSmall,$dirFileRoot,171,114,0,0,0);
				 $img_name =$this->image_library->resizeThumbnailImage($dirFileResizeHome,$dirFileRoot,383,263,0,0,0);
				 $filename==''? $img_name='':$img_name=$filename;
				 $arr_image = array('image'=>$filename); 
				 $data = $this->build_data($_POST,1);
				 $data  = array_merge($data,$arr_image);
				 if($this->news->saveData(0,$data)){
					 $this->session->set_flashdata('message','Lưu thành công');
					 redirect('news/listnews');
				 }else{
					 $this->pre_message = 'Lưu không thành công';
				 }
					 
             }
          $data['message'] = $this->pre_message;
		  $data['imagemanager_rootpath'] = $this->imagemanager_rootpath;
          $this->_templates['page'] = 'news/add';
          $this->site_lib->load($this->_templates['page'],$data);
      }
       function edit($id){    
	  		 // check quyền thêm 
	  		$this->acl->check('edit','mod_news','news',base_url());                              
            $data['title'] = 'Sửa bài viết';
            $data['query'] = $this->news->get_object($id);
            $data['option_pid'] = $this->category_news->optionCategoryLevel(0,$data['query']->category_news_id,4);
            $this->form_validation->set_rules('category_id',lang('category_news'),'trim|required');
			$this->form_validation->set_rules('title',lang('news_name'),'trim|required');
			$this->form_validation->set_rules('description',lang('news_description'),'trim|required');
			$this->form_validation->set_rules('content',lang('news_content'),'trim|required');
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
				 $dirFile = "../uploads/images/images_lib/";
				 $dirFileNew = "../uploads/images/news/small/";
				 $dirFileNewHome = "../uploads/images/news/home/";
				 $filename =$_POST['lb_image'];
				 if($filename!=''){
					 $image_old=$data['query']->image;
					 unlink("../uploads/images/news/small/".$image_old);
					 unlink("../uploads/images/news/home/".$image_old);
					 $dirFileRoot =$dirFile.$filename;
					 $dirFileResizeSmall = $dirFileNew.$filename;
					 $dirFileResizeHome = $dirFileNewHome.$filename;
					 $img_name =$this->image_library->resizeThumbnailImage($dirFileResizeSmall,$dirFileRoot,171,114,0,0,0);
					 $img_name =$this->image_library->resizeThumbnailImage($dirFileResizeHome,$dirFileRoot,383,263,0,0,0);
					 $filename==''? $img_name='':$img_name=$filename;
				 }else{
					 $img_name =$data['query']->image;
				 }
				$arr_image = array('image'=>$img_name);
				$data = $this->build_data($_POST,1);
				$data  = array_merge($data,$arr_image);
				 if($this->news->saveData($id,$data)){
					 $this->session->set_flashdata('message','Lưu thành công');
					 redirect('news/listnews');
				 }else{
					 $this->pre_message = 'Lưu không thành công';
				}
		 }
          $data['message'] = $this->pre_message;
		  $data['imagemanager_rootpath'] = $this->imagemanager_rootpath;
          $this->_templates['page'] = 'news/edit';
          $this->site_lib->load($this->_templates['page'],$data);
      }
      function del(){
	   // check quyền xóa
		$this->acl->check('del','mod_news','news',base_url());   
        $id = $this->uri->segment(3);
        $page = $this->uri->segment(4);

        if($this->news->delete($id))
            $this->session->set_flashdata('message','Đã xóa thành công');
        else $this->session->set_flashdata('message','Xóa không thành công');
        
        redirect('news/listnews/'.$page);
    }
    function dels(){
		// check quyền xóa nhiều 
		$this->acl->check('dels','mod_news','news',base_url());   
        if(!empty($_POST['ar_id']))
        {
            $page = (int)$this->input->post('page');
            $ar_id = $this->input->post('ar_id');

            if(!empty($_POST['btn_submit']))
            {
                for($i = 0; $i < sizeof($ar_id); $i ++) {
                    if ($ar_id[$i]){
                        if($this->news->delete($ar_id[$i]))
                            $this->session->set_flashdata('message','Đã xóa thành công');
                        else $this->session->set_flashdata('message','Xóa không thành công');
                    }
                }
            }
        }
        redirect('news/listnews/'.$page);
    }
    function build_data($data,$flag=1)
	{//flag=1:0?insert:update
				$dataUpdate["category_news_id"]=trim($data["category_id"]);
				$dataUpdate["title"]=trim($data["title"]);
				$dataUpdate["title_alias"]=$this->string_library->stringchange($data["title"]);
				$dataUpdate["title_en"]=trim($data["title_en"]);
				$dataUpdate["title_alias_en"]=$this->string_library->stringchange($data["title_en"]);
				$dataUpdate["description"]=trim($data["description"]);
				$dataUpdate["description_en"]=trim($data["description_en"]);
				$dataUpdate["content"]=trim($data["content"]);
				$dataUpdate["content_en"]=trim($data["content_en"]);
				$dataUpdate["seo_keyword"]=trim($data["seo_keyword"]);
				$dataUpdate["seo_keyword_en"]=trim($data["seo_keyword_en"]);
				$dataUpdate["nb_order"]=trim($data["nb_order"]);
				$dataUpdate["bl_home"]=trim($data["bl_home"]);
				$dt_public = $data["dt_public"];
				if($flag==1)
               		$dataUpdate["dt_create"] = date('Y-m-d H:i:s');
				if($dt_public){
               		$dataUpdate["dt_public"] = $data["dt_public"];
				}else{
					
					$dataUpdate["dt_public"] = date('Y-m-d H:i:s');
				}
                if(isset($data["bl_active"])){
			$dataUpdate["bl_active"]=trim($data["bl_active"]);
		}else{
			$dataUpdate["bl_active"]=0;
		}
		return $dataUpdate;
	}

  }
?>
